<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 表单编码 -->
          <el-form-item label="表单编码"
                        prop="formCode">
            <!--文本框-->
            <el-input v-model="dataForm.formCode" :disabled="dataForm.id"
                      placeholder="表单编码" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 表单名称 -->
          <el-form-item label="表单名称"
                        prop="formName">
            <!--文本框-->
            <el-input v-model="dataForm.formName"
                      placeholder="表单名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 表单分类 -->
          <el-form-item label="表单分类"
                        prop="formClass">
            <!--文本框-->
            <el-select v-model="dataForm.formClass" placeholder="表单分类" clearable>
              <el-option v-for="template in $getDictList('FORM_CLASS', true)"
                         :key="template.val"
                         :label="template.label"
                         :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 表单类型 0-设计器生成 1-自定义代码 -->
          <el-form-item label="表单类型"
                        prop="formType">
            <!--文本框-->
            <el-radio-group v-model="dataForm.formType" @change="formTypeChange">
              <el-radio-button label="0">设计器生成</el-radio-button>
              <el-radio-button label="1">自定义代码</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col v-if="contentShow" :span="24">
          <!-- 表单内容 -->
          <el-form-item label="表单内容"
                        prop="formContent">
            <!--文本框-->
            <el-input v-model="dataForm.formContent"
                      placeholder="表单内容" clearable></el-input>
          </el-form-item>
        </el-col>
<!--        <el-col :span="24">-->
<!--          &lt;!&ndash; 表单动作 &ndash;&gt;-->
<!--          <el-form-item label="表单动作"-->
<!--                        prop="formAction">-->
<!--            &lt;!&ndash;文本框&ndash;&gt;-->
<!--            <el-input v-model="dataForm.formAction"-->
<!--                      placeholder="表单动作" clearable></el-input>-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="24">
          <!-- 表单排序 -->
          <el-form-item label="表单排序"
                        prop="sort">
            <!--计数器-->
            <el-input-number v-model="dataForm.sort" :min="1" :max="50"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 表单内容 -->
          <el-form-item label="表单备注"
                        prop="remark">
            <!--文本框-->
            <el-input v-model="dataForm.remark"
                      placeholder="表单备注" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      contentShow: false,
      dataForm: {
        id: '',
        formCode: '',
        formName: '',
        formClass: '',
        formType: '0',
        formContent: '',
        formAction: '',
        sort: '',
        modelFlag: '',
        modelCode: '',
        status: '',
        remark: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        formCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        formName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        formClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        formType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        formContent: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        }
      })
    },
    formTypeChange (val) {
      if (val === '0') {
        this.contentShow = false
      } else {
        this.contentShow = true
      }
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/drs/drsFormDef/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/drs/drsFormDef/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
